package by.dkusch.aircompany.services.csv.load;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Component;

import by.dkusch.aircompany.model.tables.Airport;
import by.dkusch.aircompany.model.tables.Country;
import by.dkusch.aircompany.model.tables.Country_;
import by.dkusch.aircompany.model.tables.Region;
import by.dkusch.aircompany.model.tables.Region_;
import by.dkusch.aircompany.services.AirportService;
import by.dkusch.aircompany.services.CountryService;
import by.dkusch.aircompany.services.RegionService;
import by.dkusch.aircompany.services.csv.airportcsv.AirportCSV;
import by.dkusch.aircompany.services.csv.countrycsv.CountryCSV;
import by.dkusch.aircompany.services.csv.regioncsv.RegionCSV;

@Component
public class CsvToModelMapper {

	@Inject
	AirportService airportService;
	@Inject
	CountryService countryService;
	@Inject
	RegionService regionService;

	public AirportService getAirportService() {
		return airportService;
	}

	public void setAirportService(AirportService airportService) {
		this.airportService = airportService;
	}

	public CountryService getCountryService() {
		return countryService;
	}

	public void setCountryService(CountryService countryService) {
		this.countryService = countryService;
	}

	public RegionService getRegionService() {
		return regionService;
	}

	public void setRegionService(RegionService regionService) {
		this.regionService = regionService;
	}

	public Country convertToDBCountry(CountryCSV csv) {
		Country databaseRec = new Country();
		databaseRec.setCode(csv.getCode());
		databaseRec.setContinent(csv.getContinent());
		databaseRec.setDescription(csv.getDescription());
		databaseRec.setName(csv.getName());
		return databaseRec;
	}

	public Region convertToDBRegion(RegionCSV csv) throws Exception {
		Region databaseRec = new Region();
		databaseRec.setCode(csv.getCode());
		databaseRec.setDescription(csv.getDescription());
		databaseRec.setName(csv.getName());

		List<Country> countries = countryService.getAllByFieldRestriction(Country_.code,
				csv.getCountryR());

		if ((countries == null) || (countries.size() == 0)) {
			throw new Exception("No country for region can be found!");
		}

		databaseRec.setCountry(countries.get(0));
		databaseRec.setContinent(csv.getContinentR().substring(0, 2));
		databaseRec.setLocalCode(csv.getLocalCode());

		databaseRec.setWikipediaLink(csv.getWikipediaLink());
		return databaseRec;
	}

	public Airport convertToDBAirport(AirportCSV csv) throws Exception {
		Airport databaseRec = new Airport();
		try {
			databaseRec.setCode(csv.getCode());
			databaseRec.setContinent(csv.getContinent());
			databaseRec.setName(csv.getName());
			databaseRec.setElevationFt(csv.getElevationFt());
			databaseRec.setGpsCode(csv.getGpsCode());
			databaseRec.setHomeLink(csv.getWikipediaLink());
			databaseRec.setIataCode(csv.getIataCode());
			databaseRec.setLatitudeDeg(csv.getLatitudeDeg());
			databaseRec.setLocalCode(csv.getLocalCode());
			databaseRec.setLongitudeDeg(csv.getLongitudeDeg());
			databaseRec.setMunicipality(csv.getMunicipality());
			List<Region> regions;
			if (csv.getRegion() != null) {
				regions = regionService.getAllByFieldRestriction(Region_.code, csv.getRegion());
			} else {
				regions = regionService.getAllByFieldRestriction(Region_.code, "AD-02");
			}

			if ((regions == null) || (regions.size() == 0)) {
				System.out.println("No region for airport can be found! Region=" + csv.getRegion()
						+ " code=" + csv.getCode());
			}

			databaseRec.setRegion(regions.get(0));
			databaseRec.setScheduledService((csv.getScheduledService().trim()
					.compareToIgnoreCase("no") == 0 ? false : true));
			databaseRec.setType(csv.getType());
			databaseRec.setWikipediaLink(csv.getWikipediaLink());
		} catch (Exception e) {

		}

		return databaseRec;
	}
}
